// pages/tongxun/tongxun.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picPath: app.globalData.picPath,
    list: {},
    departments: [],
    isMusk: false,
    top: 6,
    bigText: "",
    bigStatus: false,
    activeName1: "1",
    activeName2: "1",
    palceText: '', // 搜索占位文字
    total_person: 0, // 总人数
    value: '', // 搜索内容
    text: '按首字母排序',
    show: false, // 弹窗展示
    status: 0, // 0首字母排序 1 架构排序
    memberList: [],
    indexList: [],
    totalNum: 0,
    searchList: [],
    isNo: '',
  },

  searchHandle() {
    console.log(this.data.value);
    this.getMembersList()
    this.setData({
      status: 0
    })
  },
  changeHandle() {
    if (this.data.value == '') {
      this.getMembersList()
      this.setData({
        status: 0
      })
    }
  },
  clearHandle() {
    console.log(this.data.value);
    this.getMembersList()
  },
  // 选择排序类别
  selectKind() {
    console.log(11);
    this.setData({
      show: true
    })
  },
  choose(e) {
    var flag = e.currentTarget.dataset.flag,
      status = '',
      text = '',
      show = '',
      indexList = this.data.indexList;

    status = flag;
    if (status == 0) {
      text = '按首字母排序';
      show = false;
      indexList = [];
    } else if (status == 1) {
      text = '按组织架构排序(部门)';
      show = false;
    }
    this.setData({
      status,
      text,
      indexList,
      show
    })
    this.getMembersList();
  },
  closeOverlay() {
    this.setData({
      show: false
    })
  },
  callHandle(e) {
    var tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },

  errHandle(e) {
    var index = e.currentTarget.dataset.index,
      ind = e.currentTarget.dataset.ind,
      memberList = this.data.memberList;
    memberList[index].list[ind].member_img = '/img/gerenImg/approvalTab.png'
    this.setData({
      memberList
    })
  },

  getMembersList() {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "member/getMembersList",
      that = this,
      searchList = [],
      isNo = '',
      total_person = '',
      arr = [],
      arr1 = [],
      list = [],
      memberList = [],
      map = {},
      result = [],
      indexList = [];
    app.wxRequest(url, {
      status: that.data.status,
      name: that.data.value,
      flag: 1,
    }, res => {
      console.log(res);
      if (res.flag) {
        wx.hideLoading()
        res.data.memberList = res.data.memberList.filter(item => {
          item.first_number = item.first_number.toUpperCase()
          return item.department_id != 0
        })
        var datas = res.data
        searchList = datas.memberList;
        arr = datas.departments;
        arr1 = [];
        list = datas.memberList;
        isNo = datas.memberList.length;
        total_person = datas.memberList.length;
        for (var i in arr) {
          arr[i].list = [];
          for (var j in list) {
            if (list[j].department_id == arr[i].departmentId) {
              arr[i].list.push(list[j])
            }
          }
        }
        for (var i in arr) {
          if (arr[i].parentId == 0) {
            arr1.push(arr[i])
          }
        }
        for (var i in arr) {
          for (var j in arr1) {
            if (arr1[j].departmentId == arr[i].parentId) {
              arr[i].tag = arr1[j].departmentName
            }
          }
        }

        for (var j in arr1) {
          arr1[j].list = [];
          for (var i in arr) {
            if (arr1[j].departmentName == arr[i].tag) {
              arr1[j].list = arr1[j].list.concat(arr[i].list)
            }
          }
        }
        memberList = arr1;
        memberList.forEach(ele => {
          ele.list = []
          list.forEach(lItem => {
            if (lItem.department_id == ele.departmentId) {
              ele.list.push(lItem)
            }
          })
        })

        result = datas.memberList;
        result.sort((a, b) => {
          return a['first_number'].localeCompare(b['first_number'])
        })
        for (var i in result) {
          if (map.hasOwnProperty(result[i].first_number)) {
            map[result[i].first_number].push(result[i]);
          } else {
            indexList.push(result[i].first_number);
            map[result[i].first_number] = [result[i]];
          }
        }
        console.log(indexList);
        console.log(map);
        console.log(memberList);

        that.setData({
          searchList,
          isNo,
          total_person,
          memberList,
          list: map,
          indexList
        })


        console.log(list);




      }
    })


  },
  onChange(e) {
    this.setData({
      activeName2: e.detail,
    });
  },
  onChange1(e) {
    this.setData({
      activeName1: e.detail,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMembersList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})